package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.DefaultLayoutHelper;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.QueryPageResourceRes;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/20 10:11
 * description:
 */
public class AppHomeThemeAdapter extends DelegateAdapter.Adapter<AppHomeThemeAdapter.AppHomeThemeHolder> {

    private Context mContext;
    private LayoutHelper mGridLayoutHelper;
    private QueryPageResourceRes viewData;
    private QueryPageResourceRes goverData;


    private BaseInterface baseInterface;

    public interface BaseInterface {
        public void onClick(View view, String url, String syslabl, int need_header,int modularId);
    }

    public void setBaseInterface(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }


    public AppHomeThemeAdapter(FragmentActivity activity, DefaultLayoutHelper headHelper) {
        mContext = activity;
        mGridLayoutHelper = headHelper;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mGridLayoutHelper;
    }

    @Override
    public AppHomeThemeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.apphome_theme_view, parent, false);
        return new AppHomeThemeHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(AppHomeThemeHolder holder, int position) {
        if (viewData != null) {

            if (viewData.getSub_title() != null) {
                holder.apphomeThemeTv.setText(viewData.getSub_title());
                holder.apphomeThemeTitle.setText(viewData.getMain_title());
            }

            RecyclerView apphomeThemeRv = holder.apphomeThemeRv;
            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            apphomeThemeRv.setLayoutManager(manager);
            AppHomeThemeRvAdapter themeRvAdapter = new AppHomeThemeRvAdapter(mContext);
            themeRvAdapter.setBaseInterface(new AppHomeThemeRvAdapter.BaseInterface() {
                @Override
                public void onClick(View view, String url, String syslabl, int need_header,int modularId) {

                    if (baseInterface != null) {
                        baseInterface.onClick(view, url, syslabl, need_header,  modularId);
                    }
                }
            });
            apphomeThemeRv.setAdapter(themeRvAdapter);
            themeRvAdapter.setViewData(viewData.getResource_list());
            themeRvAdapter.notifyDataSetChanged();
        }

        if (goverData != null) {
            if (goverData.getSub_title() != null) {
                holder.goverContent.setText(goverData.getSub_title());
            }
            if (goverData.getMain_title() != null) {
                holder.goverTitle.setText(goverData.getMain_title());
            }

            LinearLayoutManager mmanager = new LinearLayoutManager(mContext);
            mmanager.setOrientation(LinearLayoutManager.HORIZONTAL);
            RecyclerView apphomeGoverRv = holder.apphomeGoverRv;
            apphomeGoverRv.setLayoutManager(mmanager);

            AppHomeGoverAdapter goverAdapter = new AppHomeGoverAdapter(mContext);
            goverAdapter.setBaseInterface(new AppHomeGoverAdapter.BaseInterface() {
                @Override
                public void onClick(View view, String url, String syslabl, int need_header,int modularId) {
                    if (baseInterface != null) {
                        baseInterface.onClick(view, url, syslabl, need_header,modularId);
                    }
                }
            });
            apphomeGoverRv.setAdapter(goverAdapter);
            goverAdapter.setViewData(goverData.getResource_list());
            goverAdapter.notifyDataSetChanged();
        }
//        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public void setViewData(QueryPageResourceRes viewData) {
        this.viewData = viewData;
    }

    public void setGoverData(QueryPageResourceRes goverData) {
        this.goverData = goverData;
    }

    public static class AppHomeThemeHolder extends RecyclerView.ViewHolder {

        private final RecyclerView apphomeThemeRv;
        private final TextView apphomeThemeTv;
        private final RecyclerView apphomeGoverRv;
        private final TextView goverTitle;
        private final TextView apphomeThemeTitle;
        private final TextView goverContent;

        public AppHomeThemeHolder(View itemView) {
            super(itemView);
            apphomeThemeRv = (RecyclerView) itemView.findViewById(R.id.apphome_theme_rv);
            apphomeThemeTv = (TextView) itemView.findViewById(R.id.apphome_theme_content);
            apphomeThemeTitle = (TextView) itemView.findViewById(R.id.apphome_theme_title);
            apphomeGoverRv = (RecyclerView) itemView.findViewById(R.id.apphome_gover_rv);
            goverTitle = (TextView) itemView.findViewById(R.id.apphome_theme_govertitle);
            goverContent = (TextView) itemView.findViewById(R.id.apphome_theme_govercontent);
        }
    }

}
